<?php

// phpinfo();


require_once 'mp3\Smarty.php';
require_once 'mp3\GestionarPlaylist.php';

$smarty = new mp3_Smarty();
session_start();
require 'mp3/ComprovarSessio.php';
echo $smarty->fetch('mp3\header.tpl');


if (isset($_SESSION['userid'])){
	if (array_key_exists('pl', $_POST)){
		$playlists = array();
		if (isset ($_GET['id'])){
			$_SESSION['cancoid'] = $_GET['id'];
	
			$playlists =Mp3_GestionarPlaylist::allPlaylists($_SESSION['userid']);
		}
		$smarty->assign('playlists', $playlists);
		$smarty->assign('on', 'afegirPlaylist.php');
		echo $smarty->fetch('mp3\afegirPlaylist.tpl');
		echo $smarty->fetch('mp3\footer.tpl');
		
	} else if (array_key_exists('pl2', $_POST)){
		
		if (isset ($_GET['playlistid']) && $_GET['playlistid']>0){
			$_SESSION['playlistid'] = $_GET['playlistid'];
			$playlist = new Mp3_GestionarPlaylist($_GET['playlistid']);
			$playlist->loadXML();
		}else if (isset($_POST['titol']) && $_POST['titol']!=''){
			$playlist = new Mp3_GestionarPlaylist();
			$playlist->titol=htmlentities($_POST['titol']);
			$playlist->userid = htmlentities($_SESSION['userid']);
			$playlist->xml = $playlist->userid.$playlist->titol;
			$playlist->insert();
		}
		$playlist->afegirCanco($_SESSION['cancoid']);
		$playlist->saveXML();
		$playlist->update();
		$smarty->assign('editada', 'ok');
		echo $smarty->fetch('mp3\index.tpl');
		echo $smarty->fetch('mp3\footer.tpl');
	}
}
//	$smarty->assign('arxiu', $_POST['arxiu']);
//	$smarty->assign('canco', $canco);
//	echo $smarty->fetch('mp3\formulari.tpl');
